package com.tencent.liteav.demo.superplayer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * Created by sunxunchao on 2021/9/13.
 */
public class NetWorkStatusBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = NetWorkStatusBroadcastReceiver.class.getSimpleName();
    private OnWifiStatusListener onWifiStatusListener;

    public NetWorkStatusBroadcastReceiver(OnWifiStatusListener onWifiStatusListener) {
        this.onWifiStatusListener = onWifiStatusListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
//wifi连接上与否
//        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
//
//            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
//            if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
//
//                Log.i(TAG, "wifi断开");
//                if (onWifiStatusListener != null){
//                    onWifiStatusListener.onWifiStatus(false);
//                }
//            } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
//                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//                //获取当前wifi名称
//                Log.i(TAG, "连接到网络 " + wifiInfo.getSSID());
//                if (onWifiStatusListener != null){
//                    onWifiStatusListener.onWifiStatus(true);
//                }
//            }
//        }
//        //wifi打开与否
        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                Log.i(TAG, "系统关闭wifi");
                if (onWifiStatusListener != null){
                    onWifiStatusListener.onWifiStatus(false);
                }
            } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                Log.i(TAG, "系统开启wifi");
                if (onWifiStatusListener != null){
                    onWifiStatusListener.onWifiStatus(true);
                }
            }
        }
    }

    public static void registerNetWorkStatusChangeReceiver(Context context, NetWorkStatusBroadcastReceiver netWorkStatusBroadcastReceiver) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        context.registerReceiver(netWorkStatusBroadcastReceiver, filter);
    }

    public static void unregisterNetWorkStatusChangeReceiver(Context context, NetWorkStatusBroadcastReceiver netWorkStatusBroadcastReceiver) {
        context.unregisterReceiver(netWorkStatusBroadcastReceiver);
    }

    public interface OnWifiStatusListener{
        void onWifiStatus(boolean isOn);
    }
}
